package com.ruoyi.sqlbuilder.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.sqlbuilder.domain.SbrDatabase;
import com.ruoyi.sqlbuilder.domain.SbrExpDbInfo;
import com.ruoyi.sqlbuilder.service.IConvertSqlService;
import com.ruoyi.sqlbuilder.service.ISbrDatabaseService;
import com.ruoyi.sqlbuilder.service.ISbrExpDbInfoService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 数据库导出信息Controller
 *
 * @author yanzy
 * @date 2021-05-09
 */
@Controller
@RequestMapping("/sqlbuilder/sbrExpDbInfo")
public class SbrExpDbInfoController extends BaseController {
    private String prefix = "sqlbuilder/sbrExpDbInfo";

    @Autowired
    private ISbrExpDbInfoService sbrExpDbInfoService;
    @Autowired
    private ISbrDatabaseService sbrDatabaseService;
    @Autowired
    private IConvertSqlService convertSqlService;

    @RequiresPermissions("sqlbuilder:sbrExpDbInfo:view")
    @GetMapping()
    public String sbrExpDbInfo() {
        return prefix + "/sbrExpDbInfo";
    }

    /**
     * 查询数据库导出信息列表
     */
    @RequiresPermissions("sqlbuilder:sbrExpDbInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SbrExpDbInfo sbrExpDbInfo) {
        startPage();
        List<SbrExpDbInfo> list = sbrExpDbInfoService.selectSbrExpDbInfoList(sbrExpDbInfo);
        for (SbrExpDbInfo expDbInfo : list) {
            SbrDatabase sbrDatabase = sbrDatabaseService.selectSbrDatabaseById(expDbInfo.getDbId());
            if (null != sbrDatabase) {
                expDbInfo.setDbInfo(sbrDatabase.getHost() + ":" + sbrDatabase.getPort() + "/" + sbrDatabase.getDatabaseName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出数据库导出信息列表
     */
    @RequiresPermissions("sqlbuilder:sbrExpDbInfo:export")
    @Log(title = "数据库导出信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SbrExpDbInfo sbrExpDbInfo) {
        List<SbrExpDbInfo> list = sbrExpDbInfoService.selectSbrExpDbInfoList(sbrExpDbInfo);
        ExcelUtil<SbrExpDbInfo> util = new ExcelUtil<SbrExpDbInfo>(SbrExpDbInfo.class);
        return util.exportExcel(list, "数据库导出信息数据");
    }

    /**
     * 新增数据库导出信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存数据库导出信息
     */
    @RequiresPermissions("sqlbuilder:sbrExpDbInfo:add")
    @Log(title = "数据库导出信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SbrExpDbInfo sbrExpDbInfo) {
        return toAjax(sbrExpDbInfoService.insertSbrExpDbInfo(sbrExpDbInfo));
    }

    /**
     * 修改数据库导出信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        SbrExpDbInfo sbrExpDbInfo = sbrExpDbInfoService.selectSbrExpDbInfoById(id);
        SbrDatabase sbrDatabase = sbrDatabaseService.selectSbrDatabaseById(sbrExpDbInfo.getDbId());
        if (null != sbrDatabase) {
            sbrExpDbInfo.setDbInfo(sbrDatabase.getHost() + ":" + sbrDatabase.getPort() + "/" + sbrDatabase.getDatabaseName());
        }
        mmap.put("sbrExpDbInfo", sbrExpDbInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存数据库导出信息
     */
    @RequiresPermissions("sqlbuilder:sbrExpDbInfo:edit")
    @Log(title = "数据库导出信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SbrExpDbInfo sbrExpDbInfo) {
        return toAjax(sbrExpDbInfoService.updateSbrExpDbInfo(sbrExpDbInfo));
    }

    /**
     * 删除数据库导出信息
     */
    @RequiresPermissions("sqlbuilder:sbrExpDbInfo:remove")
    @Log(title = "数据库导出信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(sbrExpDbInfoService.deleteSbrExpDbInfoByIds(ids));
    }


    @RequiresPermissions("sqlbuilder:sbrExpDbInfo:export")
    @Log(title = "数据库导出信息", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSql")
    @ResponseBody
    public AjaxResult exportSql(String id) {
        SbrExpDbInfo sbrExpDbInfo = sbrExpDbInfoService.selectSbrExpDbInfoById(Long.valueOf(id));
        String filePath = convertSqlService.execute(sbrExpDbInfo);
        if (StringUtils.isNotBlank(filePath)) {
            return AjaxResult.success(filePath);
        }
        return AjaxResult.error("导出出错, 请查看日志");
    }
}
